//
//  ContactSearchViewController.m
//  Showing
//
//  Created by Ralf on 15/4/27.
//  Copyright (c) 2015年 next-software. All rights reserved.
//

#import "ContactSearchViewController.h"
#import "ContactSearchViewContactCell.h"
#import "AppDelegate.h"
#import "iModern-Swift.h"

@interface ContactSearchViewController ()
{
    AppDelegate *appDelegate;
    UITableView *tableViews;
}
@end

@implementation ContactSearchViewController

@synthesize contacts;

- (id)init
{
    appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
    contacts    = nil;
    return [super init];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    tableViews = (UITableView *) [self.view viewWithTag:100];
    tableViews.delegate   = self;
    tableViews.dataSource = self;

    self.title = @"搜索用户";

    [self initBackButton];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (contacts && contacts.count > 0)
    {
        return contacts.count;
    }
    else
    {
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (contacts && contacts.count > 0)
    {
        ContactSearchViewContactCell *cell = [ContactSearchViewContactCell cellWithTableView:tableView];
        [cell setData:contacts[indexPath.row]];
        return cell;
    }
    else
    {
        ContactSearchViewNotFoundCell *cell = [ContactSearchViewNotFoundCell cellWithTableView:tableView];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (contacts && contacts.count > 0)
    {
        OtherProfileViewController *controller = [OtherProfileViewController new];
        [controller loadDataByUserName:contacts[indexPath.row][@"userName"]];
        [self.navigationController pushViewController:controller animated:YES];
    }
}

- (IBAction)searchButtonOnTouchUpInside:(id)sender
{
    UITextField *tfAlias = (UITextField *) [self.view viewWithTag:20];
    NSString    *alias   = tfAlias.text;

    self.contacts = nil;

    if (alias && alias.length > 0)
    {
        [self.view endEditing:YES];
        ResultSet *rs = [appDelegate.contactManager searchByAlias:alias];

        if ([self verifyResutSet:rs] && ![rs isEmpty])
        {
            self.contacts = [rs toArray];
        }
    }

    [tableViews reloadData];
}

@end
